package com.ds.lens.data.common.util;

import com.ds.lens.data.common.constant.AggregatorEnum;

import static com.ds.lens.data.common.constant.Constants.ERROR_RATIO;

public class MetricUtils {

    private static final String METRIC_AGGR_SPLITER = "-";
    private static final String PERCENTILES = "PERCENTILES";
    private static final String PERCENTILES_SPLITER = "#";
    private static final String ERROR_RATIO_PREFIX = "a";

    public static String generateKey(String metricName, AggregatorEnum aggrMethod) {
        StringBuilder builder = new StringBuilder();
        if (ERROR_RATIO.equals(metricName)) {
            builder.append(ERROR_RATIO_PREFIX).append(METRIC_AGGR_SPLITER).append(ERROR_RATIO);
        } else if (aggrMethod == AggregatorEnum.P50 || aggrMethod == AggregatorEnum.P90 ||
                aggrMethod == AggregatorEnum.P95 || aggrMethod == AggregatorEnum.P99) {
            Integer v = Integer.valueOf(aggrMethod.name().substring(1));
            builder.append(metricName).append(METRIC_AGGR_SPLITER).append(PERCENTILES).append(PERCENTILES_SPLITER).append(v);
        } else {
            builder.append(metricName).append(METRIC_AGGR_SPLITER);
            if (aggrMethod != null) {
                builder.append(aggrMethod.name());
            }
        }
        return builder.toString();
    }
}
